/**************************************************************************/
/*! 
    @file     ILI9320.c
    @author   K. Townsend (microBuilder.eu)
    @date     22 March 2010
    @version  0.10

    @section LICENSE

    Software License Agreement (BSD License)

    Copyright (c) 2010, microBuilder SARL
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "ILI9320.h"

/*************************************************/
static void ili9320WriteCmd(uint16_t command)
{
  CLR_CS_CD;      // Saves 7 commands compared to "CLR_CS; CLR_CD;"
  SET_RD_WR;      // Saves 7 commands compared to "SET_RD; SET_WR;"
  ILI9320_GPIO2DATA_DATA = (command >> (8 - ILI9320_DATA_OFFSET));
  CLR_WR;
  SET_WR;
  ILI9320_GPIO2DATA_DATA = command << ILI9320_DATA_OFFSET;
  CLR_WR;
  SET_WR_CS;      // Saves 7 commands compared to "SET_WR; SET_CS;"
}

/*************************************************/
static void ili9320WriteData(uint16_t data)
{
  CLR_CS;
  SET_CD_RD_WR;   // Saves 14 commands compared to "SET_CD; SET_RD; SET_WR"
  ILI9320_GPIO2DATA_DATA = (data >> (8 - ILI9320_DATA_OFFSET));
  CLR_WR;
  SET_WR;
  ILI9320_GPIO2DATA_DATA = data << ILI9320_DATA_OFFSET;
  CLR_WR;
  SET_WR_CS;      // Saves 7 commands compared to "SET_WR, SET_CS;"
}

/*************************************************/
static void ili9320Command(uint16_t command, uint16_t data)
{
  // Provided for convenience sake ... shouldn't be used
  // in critical sections since it adds an extra
  // branch, etc.
  ili9320WriteCmd(command);
  ili9320WriteData(data);
}

/*************************************************/
static void ili9320Delay(unsigned int t)
{
  unsigned char t1;
  while(t--)
  for ( t1=10; t1 > 0; t1-- )
  {
    __asm("nop");
  }
}

/*************************************************/
static void ili9320InitDisplay(void)
{
  // Clear data line
  GPIO_GPIO2DATA &= ~ILI9320_DATA_MASK;
    
  SET_RD;
  SET_WR;
  SET_CS;
  SET_CD;

  // Reset display
  CLR_RESET;
  ili9320Delay(10000);
  SET_RESET;
  ili9320Delay(500);

  ili9320Command(0x00FF, 0x0001);
  ili9320Command(0x00F3, 0x0008);
  ili9320WriteCmd(0x00F3);

  ili9320Command(0x0001, 0x0100);     // Driver Output Control Register (R01h)
  ili9320Command(0x0002, 0x0700);     // LCD Driving Waveform Control (R02h)
  ili9320Command(0x0003, 0x1030);     // Entry Mode (R03h)  
  ili9320Command(0x0008, 0x0302);
  ili9320Command(0x0009, 0x0000);
  ili9320Command(0x0010, 0x0000);     // Power Control 1 (R10h)
  ili9320Command(0x0011, 0x0007);     // Power Control 2 (R11h)  
  ili9320Command(0x0012, 0x0000);     // Power Control 3 (R12h)
  ili9320Command(0x0013, 0x0000);     // Power Control 4 (R13h)
  ili9320Delay(1000);  
  ili9320Command(0x0010, 0x14B0);     // Power Control 1 (R10h)  
  ili9320Delay(500);  
  ili9320Command(0x0011, 0x0007);     // Power Control 2 (R11h)  
  ili9320Delay(500);  
  ili9320Command(0x0012, 0x008E);     // Power Control 3 (R12h)
  ili9320Command(0x0013, 0x0C00);     // Power Control 4 (R13h)
  ili9320Command(0x0029, 0x0015);     // NVM read data 2 (R29h)
  ili9320Delay(500);
  ili9320Command(0x0030, 0x0000);     // Gamma Control 1
  ili9320Command(0x0031, 0x0107);     // Gamma Control 2
  ili9320Command(0x0032, 0x0000);     // Gamma Control 3
  ili9320Command(0x0035, 0x0203);     // Gamma Control 6
  ili9320Command(0x0036, 0x0402);     // Gamma Control 7
  ili9320Command(0x0037, 0x0000);     // Gamma Control 8
  ili9320Command(0x0038, 0x0207);     // Gamma Control 9
  ili9320Command(0x0039, 0x0000);     // Gamma Control 10
  ili9320Command(0x003C, 0x0203);     // Gamma Control 13
  ili9320Command(0x003D, 0x0403);     // Gamma Control 14
  ili9320Command(0x0050, 0x0000);     // Window Horizontal RAM Address Start (R50h)
  ili9320Command(0x0051, 0x00EF);     // Window Horizontal RAM Address End (R51h)
  ili9320Command(0x0052, 0X0000);     // Window Vertical RAM Address Start (R52h)
  ili9320Command(0x0053, 0x013F);     // Window Vertical RAM Address End (R53h)
  ili9320Command(0x0060, 0xa700);     // Driver Output Control (R60h)
  ili9320Command(0x0061, 0x0001);     // Driver Output Control (R61h)
  ili9320Command(0x0090, 0X0029);     // Panel Interface Control 1 (R90h)

  // Display On
  ili9320Command(0x0007, 0x0133);     // Display Control (R07h)
  ili9320Delay(500);
  ili9320WriteCmd(0x0022);
}

/*************************************************/
void ili9320Home(void)
{
  ili9320Command(0x0020, 0X0000);     // GRAM Address Set (Horizontal Address) (R20h)
  ili9320Command(0x0021, 0X0000);     // GRAM Address Set (Vertical Address) (R21h)
  ili9320WriteCmd(0x0022);            // Write Data to GRAM (R22h)
}

/*************************************************/
void ili9320SetWindow(uint16_t x, uint16_t y, uint16_t x1, uint16_t y1)
{
  ili9320Command(0x0050, x-1);       // Window Horizontal RAM Address Start (R50h)
  ili9320Command(0x0051, x1-1);      // Window Horizontal RAM Address End (R51h)
  ili9320Command(0x0052, y-1);       // Window Vertical RAM Address Start (R52h) )
  ili9320Command(0x0053, y1-1);      // Window Vertical RAM Address End (R53h)
}

/*************************************************/
void ili9320SetCursor(uint16_t x, uint16_t y)
{
  ili9320Command(0x0020, x-1);       // GRAM Address Set (Horizontal Address) (R20h)
  ili9320Command(0x0021, y-1);       // GRAM Address Set (Vertical Address) (R21h)
}

/*************************************************/
void lcdInit(void)
{
  // Set control line pins to output
  gpioSetDir(ILI9320_CS_PORT, ILI9320_CS_PIN, 1);
  gpioSetDir(ILI9320_CD_PORT, ILI9320_CD_PIN, 1);
  gpioSetDir(ILI9320_WR_PORT, ILI9320_WR_PIN, 1);
  gpioSetDir(ILI9320_RD_PORT, ILI9320_RD_PIN, 1);
  
  // Set data port pins to output
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN1, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN2, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN3, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN4, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN5, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN6, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN7, 1);
  gpioSetDir(ILI9320_DATA_PORT, ILI9320_DATA_PIN8, 1);
    
  // Set backlight
  gpioSetDir(ILI9320_BL_PORT, ILI9320_BL_PIN, 1);      // set to output
  gpioSetValue(ILI9320_BL_PORT, ILI9320_BL_PIN, 0);    // turn on

  // Initialize the display
  ili9320InitDisplay();

  // Fill black
  lcdFillRGB(BLACK);
}

/*************************************************/
void lcdTest(void)
{
  uint32_t i,j;
  ili9320Home();
  
  for(i=0;i<320;i++)
  {
    for(j=0;j<240;j++)
    {
      if(i>279)ili9320WriteData(WHITE);
      else if(i>239)ili9320WriteData(BLUE);
      else if(i>199)ili9320WriteData(GREEN);
      else if(i>159)ili9320WriteData(CYAN);
      else if(i>119)ili9320WriteData(RED);
      else if(i>79)ili9320WriteData(MAGENTA);
      else if(i>39)ili9320WriteData(YELLOW);
      else ili9320WriteData(BLACK);
    }
  }
}

/*************************************************/
void lcdFillRGB(uint16_t data)
{
  unsigned int i;
  ili9320Home();
  
  uint32_t pixels = 320*240;
  for ( i=0; i < pixels; i++ )
  {
    ili9320WriteData(data);
  } 
}

/*************************************************/
void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
  ili9320WriteCmd(0x0020); // GRAM Address Set (Horizontal Address) (R20h)
  ili9320WriteData(x-1);
  ili9320WriteCmd(0x0021); // GRAM Address Set (Vertical Address) (R21h)
  ili9320WriteData(y-1);
  ili9320WriteCmd(0x0022);  // Write Data to GRAM (R22h)
  ili9320WriteData(color);
}
